<template>
  <div class="login-container">
    <div class="login-left">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
      </div>
      <div class="brand">
        <div class="logo-animation">
          <div class="circle"></div>
          <div class="circle circle-2"></div>
          <i class="el-icon"><Document /></i>
        </div>
        <h1>协作表格</h1>
        <p>让团队协作更高效</p>
      </div>
      
      <div class="features">
        <div v-for="(feature, index) in features" 
             :key="feature.title"
             class="feature-item"
             :style="{ animationDelay: `${index * 0.1}s` }">
          <div class="feature-icon">
            <el-icon><component :is="feature.icon" /></el-icon>
          </div>
          <div class="feature-text">
            <h3>{{ feature.title }}</h3>
            <p>{{ feature.description }}</p>
          </div>
        </div>
      </div>
    </div>

    <div class="login-right">
      <div class="login-box">
        <div v-show="!isRegister" class="form-container">
          <div class="header">
            <h2>登录</h2>
            <p>还没有账号？<a href="#" @click.prevent="toggleForm">立即注册</a></p>
          </div>
          
          <el-form :model="loginForm" class="login-form">
            <div class="form-group">
              <label class="form-label">用户名</label>
              <el-input 
                v-model="loginForm.email" 
                placeholder="请输入用户名"
                :prefix-icon="User"
                size="large"
              />
            </div>
            
            <div class="form-group">
              <label class="form-label">密码</label>
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="请输入密码"
                :prefix-icon="Lock"
                size="large"
                show-password
              />
            </div>
            
            <div class="form-options">
              <el-checkbox v-model="rememberMe">
                <span class="checkbox-text">记住我</span>
              </el-checkbox>
            </div>
            
            <el-button type="primary" class="login-button" size="large" @click="handleLogin">
              登录
              <el-icon class="el-icon--right"><ArrowRight /></el-icon>
            </el-button>
          </el-form>
        </div>

        <div v-show="isRegister" class="form-container">
          <div class="header">
            <h2>注册账号</h2>
            <p>已有账号？<a href="#" @click.prevent="toggleForm">立即登录</a></p>
          </div>
          
          <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            class="register-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="registerForm.username"
                placeholder="用户名"
                :prefix-icon="User"
              />
            </el-form-item>

            <el-form-item prop="email">
              <el-input
                v-model="registerForm.email"
                placeholder="邮箱"
                :prefix-icon="Message"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                placeholder="密码"
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="确认密码"
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>

            <el-button type="primary" class="submit-btn" @click="handleRegister">
              注册
            </el-button>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { User, Lock, Document, ArrowRight, Message } from '@element-plus/icons-vue';
import {getLogin,getRegister} from '@/api/user/index.ts'
import store from '@/stores/module/user.ts'
const userStore = store()
const router = useRouter();
const loginForm = ref({
  email: '',
  password: ''
});
const rememberMe = ref(false);

const features = [
  {
    icon: 'Share',
    title: '实时协作',
    description: '多人同时编辑，变更实时同步'
  },
  {
    icon: 'Edit',
    title: '在线编辑',
    description: '随时随地，轻松编辑文档'
  },
  {
    icon: 'DataAnalysis',
    title: '数据分析',
    description: '强大的公式和图表功能'
  }
];

const isRegister = ref(false);
const registerFormRef = ref();
const registerForm = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
});

const validatePass = (_rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else {
    if (registerForm.value.confirmPassword !== '') {
      registerFormRef.value?.validateField('confirmPassword');
    }
    callback();
  }
};

const validatePass2 = (_rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerForm.value.password) {
    callback(new Error('两次输入密码不一致!'));
  } else {
    callback();
  }
};

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ]
};

const toggleForm = () => {
  isRegister.value = !isRegister.value;
};

const handleRegister = async () => {
  if (!registerFormRef.value) return;
  
  await registerFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        // 这里调用注册接口
        const res = await getRegister({user:registerForm.value});
        if(res.code == 200){
          ElMessage.success('注册成功！');
          isRegister.value = false; // 切换回登录表单
        }
      } catch (error: any) {
        ElMessage.error(error.message || '注册失败');
      }
    }
  });
};

const handleLogin = async () => {
  if (!loginForm.value.email || !loginForm.value.password) {
    ElMessage.warning('请输入用户名和密码');
    return;
  }
  try {
    const res = await getLogin({user:loginForm.value});
    if(res.code == 200){
      userStore.accessToken = res.accessToken
      userStore.refreshToken = res.refreshToken
      userStore.user = res.data
      localStorage.setItem('user', JSON.stringify(userStore.user))
      localStorage.setItem('FRONTEND_ACCESS_TOKEN', userStore.accessToken)
      localStorage.setItem('FRONTEND_REFRESH_TOKEN', userStore.refreshToken)
      ElMessage.success('登录成功');
      router.push('/dashboard');
    }
  } catch (error: any) {
    ElMessage.error(error.message || '登录失败');
  }
};
</script>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  background: white;
  
  .login-left {
    flex: 1;
    background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
    padding: 60px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    position: relative;
    overflow: hidden;
    
    .floating-shapes {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      pointer-events: none;
      
      .shape {
        position: absolute;
        background: rgba(255, 255, 255, 0.1);
        backdrop-filter: blur(5px);
        border-radius: 50%;
        animation: float 20s infinite linear;
        
        &-1 {
          width: 200px;
          height: 200px;
          top: 10%;
          left: 10%;
          animation-delay: -5s;
        }
        
        &-2 {
          width: 150px;
          height: 150px;
          top: 60%;
          right: 15%;
          animation-delay: -10s;
        }
        
        &-3 {
          width: 100px;
          height: 100px;
          bottom: 20%;
          left: 20%;
          animation-delay: -15s;
        }
      }
    }
    
    .brand {
      text-align: center;
      position: relative;
      z-index: 1;
      animation: fadeInDown 1s ease-out;
      
      .logo-animation {
        position: relative;
        width: 120px;
        height: 120px;
        margin: 0 auto 30px;
        
        .circle {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          border-radius: 30px;
          background: linear-gradient(135deg, #43a047 0%, #66bb6a 100%);
          animation: pulse 2s ease-in-out infinite;
          
          &-2 {
            opacity: 0.5;
            animation-delay: -1s;
          }
        }
        
        .el-icon {
          position: relative;
          font-size: 48px;
          color: white;
          width: 120px;
          height: 120px;
          display: flex;
          align-items: center;
          justify-content: center;
          animation: bounce 2s ease-in-out infinite;
        }
      }
      
      h1 {
        color: #2e7d32;
        font-size: 42px;
        margin: 0 0 16px;
        font-weight: 700;
        text-shadow: 0 2px 4px rgba(0,0,0,0.1);
      }
      
      p {
        color: #558b2f;
        font-size: 20px;
        margin: 0;
        opacity: 0.9;
      }
    }
    
    .features {
      position: relative;
      z-index: 1;
      
      .feature-item {
        display: flex;
        align-items: center;
        gap: 24px;
        padding: 24px;
        background: rgba(255, 255, 255, 0.2);
        backdrop-filter: blur(10px);
        border-radius: 20px;
        margin-bottom: 20px;
        transition: all 0.3s;
        animation: slideInLeft 0.5s ease-out forwards;
        opacity: 0;
        
        &:hover {
          transform: translateX(10px) scale(1.02);
          background: rgba(255, 255, 255, 0.3);
          
          .feature-icon {
            transform: rotate(10deg);
          }
        }
        
        .feature-icon {
          font-size: 32px;
          color: #43a047;
          background: white;
          width: 70px;
          height: 70px;
          border-radius: 20px;
          display: flex;
          align-items: center;
          justify-content: center;
          transition: transform 0.3s;
          box-shadow: 0 4px 12px rgba(67, 160, 71, 0.2);
        }
        
        .feature-text {
          h3 {
            color: #2e7d32;
            margin: 0 0 8px;
            font-size: 20px;
            font-weight: 600;
          }
          
          p {
            color: #558b2f;
            margin: 0;
            font-size: 15px;
            opacity: 0.9;
            line-height: 1.5;
          }
        }
      }
    }
  }
  
  .login-right {
    width: 500px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
    position: relative;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 10%;
      bottom: 10%;
      width: 2px;
      background: linear-gradient(to bottom, 
        transparent,
        rgba(67, 160, 71, 0.5),
        transparent
      );
      filter: blur(1px);
    }
    
    .login-box {
      width: 100%;
      max-width: 400px;
      animation: fadeIn 1s ease-out;
      position: relative;
      
      .header {
        text-align: center;
        position: relative;
        margin-bottom: 40px;
        p{
          a{
            color: #43a047;
            text-decoration: underline;
          }
        }
        .glowing-circle {
          position: absolute;
          width: 100px;
          height: 100px;
          background: radial-gradient(circle at center,
            rgba(67, 160, 71, 0.2),
            transparent 70%
          );
          top: -20px;
          left: 50%;
          transform: translateX(-50%);
          filter: blur(20px);
          animation: glow 3s ease-in-out infinite;
        }
        
        h2 {
          color: #2e7d32;
          font-size: 36px;
          margin: 0 0 12px;
          font-weight: 700;
          background: linear-gradient(135deg, #2e7d32, #66bb6a);
          -webkit-background-clip: text;
          -webkit-text-fill-color: transparent;
          position: relative;
        }
        
        .subtitle {
          color: #558b2f;
          font-size: 16px;
          opacity: 0.8;
        }
      }
      
      .form-group {
        margin-bottom: 24px;
        
        .form-label {
          display: block;
          margin-bottom: 8px;
          color: #2e7d32;
          font-weight: 500;
          font-size: 14px;
        }
        
        :deep(.el-input) {
          --el-input-hover-border-color: #43a047;
          --el-input-focus-border-color: #43a047;
          
          .el-input__wrapper {
            background: rgba(255, 255, 255, 0.8);
            box-shadow: 0 2px 8px rgba(76, 175, 80, 0.1);
            transition: all 0.3s;
            border-radius: 12px;
            border: 1px solid transparent;
            
            &:hover {
              background: rgba(255, 255, 255, 0.95);
            }
            
            &.is-focus {
              transform: translateY(-1px);
              box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
              border-color: rgba(67, 160, 71, 0.3);
              background: white;
            }
          }
          
          .el-input__prefix-inner {
            color: #43a047;
          }
        }
        
        .input-status {
          width: 6px;
          height: 6px;
          border-radius: 50%;
          background: #e8f5e9;
          transition: all 0.3s;
          
          .is-focus & {
            background: #43a047;
            transform: scale(1.2);
          }
        }
      }
      
      .form-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 24px;
        
        .checkbox-text {
          color: #558b2f;
          font-size: 14px;
        }
        
        .forget-link {
          font-size: 14px;
          position: relative;
          overflow: hidden;
          
          &::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 1px;
            background: currentColor;
            transform: translateX(-101%);
            transition: transform 0.3s ease;
          }
          
          &:hover::after {
            transform: translateX(0);
          }
        }
      }
      
      .button-group {
        margin-bottom: 32px;
        
        .login-button {
          width: 100%;
          height: 48px;
          font-size: 16px;
          background: linear-gradient(135deg, #43a047 0%, #66bb6a 100%);
          border: none;
          border-radius: 12px;
          position: relative;
          overflow: hidden;
          
          .button-content {
            position: relative;
            z-index: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            
            .button-text, .button-icon {
              transition: transform 0.3s;
            }
          }
          
          .button-glow {
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle at center,
              rgba(255, 255, 255, 0.3),
              transparent 70%
            );
            opacity: 0;
            transition: opacity 0.3s;
          }
          
          &:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
            
            .button-text {
              transform: translateX(-4px);
            }
            
            .button-icon {
              transform: translateX(4px);
            }
            
            .button-glow {
              opacity: 1;
              animation: rotate 2s linear infinite;
            }
          }
        }
      }
      
      .social-login {
        display: flex;
        justify-content: center;
        gap: 20px;
        margin-bottom: 32px;
        
        .social-button {
          width: 50px;
          height: 50px;
          border-radius: 15px;
          border: 1px solid #e8f5e9;
          background: white;
          cursor: pointer;
          position: relative;
          transition: all 0.3s;
          
          i {
            font-size: 20px;
            transition: all 0.3s;
          }
          
          .hover-text {
            position: absolute;
            top: 100%;
            left: 50%;
            transform: translateX(-50%);
            padding: 4px 8px;
            background: rgba(0, 0, 0, 0.8);
            color: white;
            font-size: 12px;
            border-radius: 4px;
            opacity: 0;
            transition: all 0.3s;
            pointer-events: none;
            white-space: nowrap;
          }
          
          &:hover {
            transform: translateY(-3px);
            border-color: currentColor;
            
            .hover-text {
              opacity: 1;
              transform: translateX(-50%) translateY(8px);
            }
          }
          
          &.github {
            color: #333;
            &:hover { color: #24292e; }
          }
          
          &.google {
            color: #ea4335;
            &:hover { color: #db4437; }
          }
          
          &.wechat {
            color: #07c160;
            &:hover { color: #06ad56; }
          }
        }
      }
      
      .register-link {
        text-align: center;
        color: #666;
        font-size: 14px;
        
        .register-btn {
          font-weight: 500;
          position: relative;
          margin-left: 4px;
          
          .link-underline {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 1px;
            background: currentColor;
            transform: scaleX(0);
            transition: transform 0.3s;
            transform-origin: right;
          }
          
          &:hover .link-underline {
            transform: scaleX(1);
            transform-origin: left;
          }
        }
      }
    }
  }
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(30px, 20px) rotate(180deg);
  }
  100% {
    transform: translate(0, 0) rotate(360deg);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1) rotate(-10deg);
  }
  50% {
    transform: scale(1.05) rotate(-10deg);
  }
  100% {
    transform: scale(1) rotate(-10deg);
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes glow {
  0%, 100% {
    transform: translateX(-50%) scale(1);
    opacity: 0.5;
  }
  50% {
    transform: translateX(-50%) scale(1.2);
    opacity: 0.8;
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.form-container {
  animation: fadeIn 0.3s ease-out;
}

.register-form {
  .submit-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    background: var(--primary-gradient);
    border: none;
    border-radius: 12px;
    margin-top: 24px;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
    }
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style> 